/*
 *                        .::::.
 *                      .::::::::.
 *                     :::::::::::
 *                  ..:::::::::::'
 *               '::::::::::::'
 *                 .::::::::::
 *            '::::::::::::::..
 *                 ..::::::::::::.
 *               ``::::::::::::::::
 *                ::::``:::::::::'        .:::.
 *               ::::'   ':::::'       .::::::::.
 *             .::::'      ::::     .:::::::'::::.
 *            .:::'       :::::  .:::::::::' ':::::.
 *           .::'        :::::.:::::::::'      ':::::.
 *          .::'         ::::::::::::::'         ``::::.
 *      ...:::           ::::::::::::'              ``::.
 *     ````':.          ':::::::::'                  ::::..
 *                        '.:::::'                    ':'````..
 *
 * @Author: byx
 * @Date: 2022-11-29 13:22:44
 * @LastEditors: byx
 * @LastEditTime: 2022-11-29 17:10:49
 * @version: 1.0
 * @Descripttion: wwww.byx1024.top
 */
import React, { Component } from 'react'
import { Redirect, Route } from 'react-router-dom'
import { hasToken } from 'utils/storage'

export default class AuthRoute extends Component {
  render() {
    const { component: Component, ...rest } = this.props

    return (
      <Route
        {...rest}
        render={routeProps => {
          if (hasToken()) {
            // 有token，走登录
            return <Component {...routeProps}></Component>
          } else {
            // 如果没有token，没有登录，渲染Redirect 重定向组件，跳转到 /login
            return (
              <Redirect
                to={{
                  pathname: '/login',
                  // 通过 search 传递参数
                  // search:'?id=123'
                  state: {
                    from: routeProps.location.pathname,
                  },
                }}
              ></Redirect>
            )
          }
        }}
      ></Route>
    )
  }
}
